package client;

import client.handler.ResponseHandler;
import client.handler.ResponseHandlerFactory;
import client.request.RequestWorker;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import server.Connector;
import unit.response.Response;
import view.ClientInterface;
import view.swinginterface.SwingInterface;

import java.io.IOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;

/**
 * User: Innokenty
 * Date: 08.08.11
 */
public class Client {
    private Logger log = Logger.getLogger(Client.class);
    public ClientInterface clientInterface;

    public Connector connector;
    private ResponseHandlerFactory handlerFactory;
    public RequestWorker requestWorker;

    private Thread worker = null;
    private boolean stopped = false;

    public static long DELAY = 1000;
    public int currentSession;
    public int clientID;

    public Client(String host, int port) {
        try {
            connector = new Connector(host, port);
        } catch (IOException e) {
            log.debug("Could not establish connection with " + host + ":" + port);
            return;
        }

        handlerFactory = new ResponseHandlerFactory(this);
        requestWorker = new RequestWorker(this);
        clientInterface = new SwingInterface(requestWorker);

        worker = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!stopped) {
                    synchronized (connector) {
                        try {
                            Response response = (Response) connector.read();
                            if (response != null)
                                handleResponse(response);
                            connector.wait(DELAY);
                        } catch (SocketTimeoutException e) {
                            log.error("Socket exceeded it's timeout");
                        } catch (SocketException e) {
                            log.error("Socket disconnected");
                            return;
                        } catch (IOException e) {
                            log.error(e);
                            return;
                        } catch (InterruptedException e) {
                            log.error("Connection interrupted");
                            return;
                        }
                    }
                }
            }
        });
        worker.start();
    }

    public void handleResponse(Response response) {
        ResponseHandler handler = handlerFactory.getHandler(response.getClass());
        handler.handleRequest(response);
    }

    public static void main(String[] args) throws IOException {
        PropertyConfigurator.configure("Client/res/log4j.properties");
        Client client = new Client("localhost", 2222);
        //client.connector.send(new SendMessageRequest(1, 1, new TextMessage("sadfsa", 1, 1)));

    }
}
